package com.totoro.utils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.Locale;

/**
 * @author TOTORO
 * @date 2021/8/28 13:32
 * 仿微信聊天记录时间工具
 */
public class WechatTimeUtil {
        /**
         * 仿微信聊天时间格式化
         *
         * @param time 时间戳
         * @return
         */
        public static String weChatFormatTime(long time) {
            Date date = new Date();
            date.setTime(time);
            //同一年 显示MM-dd HH:mm
            if (isSameYear(date)) {
                SimpleDateFormat simpleDateFormat = new SimpleDateFormat("HH:mm", Locale.CHINA);
                //同一天 显示HH:mm
                if (isSameDay(date)) {
                    int minute = minutesAgo(time);
                    //5分钟内，显示刚刚
                    if (minute < 5) {
                        return "刚刚";
                    } else {
                        return ifAfternoonByDate(date) +" "+simpleDateFormat.format(date);
                    }
                } else {
                    //昨天，显示昨天+HH:mm
                    if (isYesterday(date)) {
                        return "昨天 " + simpleDateFormat.format(date);
                    }
                    //本周,显示周几+HH:mm
                    else if (isSameWeek(date)) {
                        String weekday = null;
                        if (date.getDay() == 1) {
                            weekday = "星期一";
                        }
                        if (date.getDay() == 2) {
                            weekday = "星期二";
                        }
                        if (date.getDay() == 3) {
                            weekday = "星期三";
                        }
                        if (date.getDay() == 4) {
                            weekday = "星期四";
                        }
                        if (date.getDay() == 5) {
                            weekday = "星期五";
                        }
                        if (date.getDay() == 6) {
                            weekday = "星期六";
                        }
                        if (date.getDay() == 0) {
                            weekday = "星期日";
                        }
                        return weekday + " " +ifAfternoonByDate(date) + " " +simpleDateFormat.format(date);
                    } else {//同一年 显示MM-dd HH:mm
                        SimpleDateFormat sdf = new SimpleDateFormat("MM-dd HH:mm", Locale.CHINA);
                        return sdf.format(date);
                    }
                }
            } else {//不是同一年 显示完整日期yyyy-MM-dd HH:mm
                SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm", Locale.CHINA);
                return sdf.format(date);
            }
        }

        /**
         * 几分钟前
         *
         * @param time
         * @return
         */
        public static int minutesAgo(long time) {
            return (int) ((System.currentTimeMillis() - time) / (1000 * 60));
        }

        /**
         * 与当前时间是否在同一周
         * 先判断是否在同一年，然后根据Calendar.DAY_OF_YEAR判断所得的周数是否一致
         *
         * @param date
         * @return
         */
        public static boolean isSameWeek(Date date) {
            if (isSameYear(date)) {
                Calendar ca = Calendar.getInstance();
                //西方周日为一周的第一天，咱得将周一设为一周第一天
                ca.setFirstDayOfWeek(Calendar.MONDAY);
                ca.setTime(date);
                Calendar caNow = Calendar.getInstance();
                caNow.setFirstDayOfWeek(Calendar.MONDAY);
                caNow.setTime(new Date());
                if (ca.get(Calendar.WEEK_OF_YEAR) == caNow.get(Calendar.WEEK_OF_YEAR)) {
                    return true;
                }
            } else {
                return false;
            }
            return false;
        }

        /**
         * 是否是当前时间的昨天
         * 获取指定时间的后一天的日期，判断与当前日期是否是同一天
         *
         * @param date
         * @return
         */
        public static boolean isYesterday(Date date) {
            Date yesterday = getNextDay(date, 1);
            return isSameDay(yesterday);
        }

        /**
         * 判断与当前日期是否是同一天
         *
         * @param date
         * @return
         */
        public static boolean isSameDay(Date date) {
            return isEquals(date, "yyyy-MM-dd");
        }

        /**
         * 判断与当前日期是否是同一月
         *
         * @param date
         * @return
         */
        public static boolean isSameMonth(Date date) {
            return isEquals(date, "yyyy-MM");
        }

        /**
         * 判断与当前日期是否是同一年
         *
         * @param date
         * @return
         */
        public static boolean isSameYear(Date date) {
            return isEquals(date, "yyyy");
        }


        /**
         * 格式化Date，判断是否相等
         *
         * @param date
         * @return 是返回true，不是返回false
         */
        private static boolean isEquals(Date date, String format) {
            //当前时间
            Date now = new Date();
            SimpleDateFormat sf = new SimpleDateFormat(format);
            //获取今天的日期
            String nowDay = sf.format(now);
            //对比的时间
            String day = sf.format(date);
            return day.equals(nowDay);
        }

        /**
         * 获取某个date第n天的日期
         * n<0 表示前n天
         * n=0 表示当天
         * n>1 表示后n天
         *
         * @param date
         * @param n
         * @return
         */
        public static Date getNextDay(Date date, int n) {
            Calendar calendar = Calendar.getInstance();
            calendar.setTime(date);
            calendar.add(Calendar.DAY_OF_MONTH, n);
            date = calendar.getTime();
            return date;
        }

    public static String ifAfternoonByDate(Date date) {
        SimpleDateFormat df = new SimpleDateFormat("HH");
        String str = df.format(date);
        int hour = Integer.parseInt(str);
        if (hour > 0 && hour < 13) {
            return "上午";
        }
        return "下午";
    }

    }
